/**
 * 
 */
package com.fyp.ptma.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * @author YahyaArshad
 * 
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "nameSpace" }))
@XmlRootElement(name = "application")
@XmlAccessorType(XmlAccessType.NONE)
public class Application implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@XmlElement
	private String applicationName;
	private String aboutApp;

	private User user;

	private List<Group> groups = new ArrayList<Group>();

	@XmlElement
	private Long id;
	@XmlElement
	private String nameSpace;
	@XmlElement(name = "version")
	@XmlElementWrapper(name = "versions")
	private List<ApplicationVersion> appVersions = new ArrayList<ApplicationVersion>(
			5);

	private List<Downloader> downloaders = new ArrayList<Downloader>();

	@ManyToMany(fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.SAVE_UPDATE })
	@Column(nullable = true)
	@JoinTable(name = "ApplicationsDownloaders", joinColumns = { @JoinColumn(referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(referencedColumnName = "id") })
	public List<Downloader> getDownloaders() {
		return downloaders;
	}

	public void setDownloaders(List<Downloader> downloaders) {
		this.downloaders = downloaders;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(referencedColumnName = "id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Id
	@GeneratedValue
	@Column(nullable = false, updatable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.ALL })
	@Column(nullable = true)
	@JoinTable(name = "ApplicationGroup", joinColumns = { @JoinColumn(referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(referencedColumnName = "id") })
	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	@Column(nullable = false)
	public String getApplicationName() {
		return applicationName;
	}

	public void setApplicationName(String applicationName) {
		this.applicationName = applicationName;
	}

	@Column(length = 1000)
	public String getAboutApp() {
		return aboutApp;
	}

	public void setAboutApp(String aboutApp) {
		this.aboutApp = aboutApp;
	}

	public String getNameSpace() {
		return nameSpace;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	@OneToMany(mappedBy = "versionPk.application")
	@Cascade(value = { CascadeType.ALL })
	@LazyCollection(LazyCollectionOption.FALSE)
	public List<ApplicationVersion> getAppVersions() {
		return appVersions;
	}

	public void setAppVersions(List<ApplicationVersion> appVersions) {
		this.appVersions = appVersions;
	}

	public Application(String nameSpace, String appName) {

		this.nameSpace = nameSpace;
		this.applicationName = appName;
	}

	public Application(User user, String appName, String nameSpace) {
		this.user = user;
		this.nameSpace = nameSpace;
		this.applicationName = appName;

	}

	public Application() {

	}

}
